<template>
    <div>
      <!-- 使用el-row和el-col来创建网格系统 -->
      <el-row :gutter="20">
        <el-col :xs="24" :sm="24" :md="24" :lg="8">
          <el-form ref="form" :model="form" label-width="80px">
            <el-form-item label="成品数量">
              <el-input v-model="form.chengPinAll"></el-input>
            </el-form-item>
            <el-form-item label="合格数量">
              <el-input v-model="form.qualifiedNum"></el-input>
            </el-form-item>
            <el-form-item label="不合格数量">
              <el-input v-model="form.unNum"></el-input>
            </el-form-item>
            <el-form-item label="未使用原料数量">
              <el-input v-model="form.unuse"></el-input>
            </el-form-item>
            <!-- 为按钮组添加一个容器，并使用flex布局居中 -->
            <div style="display: flex; justify-content: center;">
              <el-form-item>
                <el-button type="primary" @click="onSubmit">确定下工</el-button>
                <el-button>取消</el-button>
              </el-form-item>
            </div>
          </el-form>
        </el-col>
      </el-row>
    </div>
  </template>
  
  <script>
  import { xiaGong, getClaim } from '@/api/dayOrder';
  
  export default {
    data() {
      return {
        claim: "",
        form: {
          name: '',
          chengPinAll: '',
          qualifiedNum: '',
          unNum: '',
          unuse: ''
        }
      }
    },
    created() {
      this.fetchClaim();
    },
    methods: {
      fetchClaim() {
        this.form.name = this.$route.params.name;
        getClaim(this.form.name) // 使用getClaim方法获取数据
          .then(response => {
            // 更新响应式数据
            this.claim = response.data.data;
          })
          .catch(error => {
            console.error('请求错误:', error);
          });
      },
      onSubmit() {
        // 验证成品数量是否大于领用原料数
        if (parseInt(this.form.chengPinAll, 10) > parseInt(this.claim, 10)) {
          this.$message.error('数据不合理：产量大于领用原料数。');
          return;
        }
  
        // 验证成品数量是否等于合格数量加上不合格数量
        const totalProduced = parseInt(this.form.qualifiedNum, 10) + parseInt(this.form.unNum, 10);
        if (parseInt(this.form.chengPinAll, 10) !== totalProduced) {
          this.$message.error('数据不合理：成品数量应等于合格数量加上不合格数量。');
          return;
        }
  
        // 验证领用原料与生产数量是否对应
        const totalAccounted = parseInt(this.form.chengPinAll, 10) + parseInt(this.form.unuse, 10);
        if (parseInt(this.claim, 10) !== totalAccounted) {
          this.$message.error('数据不合理：领用原料与生产数量不对应。');
          return;
        }
  
        this.form.name = this.$route.params.name;
        console.log('submit!');
        xiaGong(this.form);
        this.$router.push({ path: '/other/xiaGongResult' });
      }
    }
  }
  </script>
  
  <style scoped>
  /* 添加一些额外的样式来改善间距 */
  .el-form-item {
    margin-bottom: 15px;
  }
  
  .el-button {
    margin-right: 10px;
  }
  
  /* 适配小屏幕设备 */
  @media (max-width: 767px) {
    .el-form-item {
      label {
        display: block;
        margin-bottom: 5px;
        text-align: left;
      }
    }
  
    .el-input {
      width: 100%;
    }
  
    .el-button {
      width: 100%;
      margin-bottom: 10px;
      margin-right: 0;
    }
  
    .el-button:last-child {
      margin-bottom: 0;
    }
  
    /* 居中对齐按钮 */
    .el-row > .el-col > .el-form > div {
      display: flex;
      justify-content: center;
      flex-direction: column;
    }
  }
  </style>